package com.ray.auth.system.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ray.auth.system.emun.MenuType;
import com.ray.auth.system.service.ButtonService;
import com.ray.auth.system.service.DictService;
import com.ray.auth.system.service.MenuService;
import com.ray.auth.system.table.entity.Button;
import com.ray.auth.system.table.entity.Dict;
import com.ray.auth.system.table.entity.Menu;
import com.ray.common.util.DataGridUtil;
import com.ray.common.util.DictLoadUtil;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.web.controller.WebController;

/**
 * <h1>资源 控制器 </h1>
 * @创建人 ray
 * @创建时间 2017-07-31
 * @版本 1.0
 * @重要修改备注 
 * 
 */
@Controller
@RequestMapping("/system/resources")
public class ResourcesController extends WebController{
	@Autowired
	private MenuService menuService;
	@Autowired
	private ButtonService buttonService;
	@Autowired
	private DictService dictService;
	@Autowired 
	private DictLoadUtil DictLoadUtil;
	/**
	 * 租户页面跳转
	 * @return
	 */
	@RequestMapping(value = "view")
	@RequiresPermissions("resource:view")
	public ModelAndView view(String menuId, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.setViewName("system/resources/view");
	  return modelAndView;
	}
	/**
	 * 新增页面
	 * @return
	 */
	@RequestMapping(value = "menuAddView")
	@RequiresPermissions("resource:menu:add")
	public ModelAndView menuAddView(String menuId,String parentMenuName,Menu record, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	 //获取字典信息
	  Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(dictService,"useStatus","menuType");
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.addObject("parentMenuName", parentMenuName);
	  modelAndView.addObject("record", record);
	  modelAndView.addObject("dicts", dicts);
	  modelAndView.setViewName("system/resources/menu/add");
	  return modelAndView;
	}
	
	/**
	 * 菜单修改页面
	 * @return
	 */
	@RequestMapping(value = "menuUpdateView")
	@RequiresPermissions("resource:menu:update")
	public ModelAndView menuUpdateView(String menuId,String parentMenuId,String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	//获取字典信息
	  Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(dictService,"useStatus","menuType");
	  //查询菜单
	  Menu record = menuService.queryMenu(id,user); 
	  //获取父节点
	  Menu parentRecord = menuService.queryParentMenu(parentMenuId,user);
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.addObject("parentMenuName", parentRecord.getMenuName());
	  modelAndView.addObject("record", record);
	  modelAndView.addObject("dicts", dicts);
	  modelAndView.setViewName("system/resources/menu/update");
	  return modelAndView;
	}
	
	/**
	 * 新增页面
	 * @return
	 */
	@RequestMapping(value = "menuView")
	@RequiresPermissions("resource:menu:view")
	public ModelAndView menuView(String menuId,String parentMenuName,String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  //查询菜单
	  Menu record = menuService.queryMenu(id,user); 
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.addObject("parentMenuName", parentMenuName);
	  modelAndView.addObject("record", record);
	  modelAndView.setViewName("system/resources/menu/view");
	  return modelAndView;
	}
	
	/**
	 * 更新方法
	 * @return
	 */
	@RequestMapping(value = "menuUpdate")
	@RequiresPermissions("resource:menu:update")
	public ResponseEntity<Result<Object>> menuUpdate(Menu record, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  Result<Object> result = menuService.updateMenu(record, user);
	  return doResult(result);
	}
	
	/**
	 * 新增方法
	 * @return
	 */
	@RequestMapping(value = "menuAdd")
	@RequiresPermissions("resource:menu:add")
	public ResponseEntity<Result<Object>> menuAdd(Menu record, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  Result<Object> result = menuService.saveMenu(record, user);
	  return doResult(result);
	}
	
	/**
	 * 资源分页查询
	 * @return
	 */
	@RequestMapping(value = "query")
	@RequiresPermissions("resource:query")
	@ResponseBody
	public ResponseEntity<Map<String,Object>> query(Menu params, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		PageDto<Menu>  page = this.createJQPage(request);
		page = menuService.queryMenuList(params, page, loginUser);
		return doSuccess(DataGridUtil.dataConvert(page));
	}
	/**
	 * 资源-按钮分页查询
	 * @return
	 */
	@RequestMapping(value = "buttonQuery")
	@RequiresPermissions("resource:buttonQuery")
	@ResponseBody
	public ResponseEntity<Map<String,Object>> buttonQuery(Button params, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		PageDto<Button> page = this.createJQPage(request);
		page = buttonService.queryButtonList(params, page, loginUser);
		return doSuccess(DataGridUtil.dataConvert(page));
	}
	
	/**
	 * 菜单删除
	 * @return
	 */
	@RequestMapping(value = "menuDelete")
	@RequiresPermissions("resource:menu:delete")
	@ResponseBody
	public ResponseEntity<Result<Object>> menuDelete(String id, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		Result<Object> result = menuService.deleteMenu(id,loginUser);
		return doResult(result);
	}
	
	/**
	 * 菜单删除
	 * @return
	 */
	@RequestMapping(value = "buttonDelete")
	@RequiresPermissions("resource:button:delete")
	@ResponseBody
	public ResponseEntity<Result<Object>> buttonDelete(String id, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		Result<Object> result = buttonService.deleteButton(id,loginUser);
		return doResult(result);
	}
	
	/**
	 * 新增页面
	 * @return
	 */
	@RequestMapping(value = "buttonAddView")
	@RequiresPermissions("resource:button:add")
	public ModelAndView buttonAddView(String mId,String menuName,Button record, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,mId);
	  //新增字典
	  List<Menu> list = menuService.queryMenusByMenuType(MenuType.GN.getType(),user);
	  DictLoadUtil.addMenuDict(list,"menus");
	  //获取字典信息
	  Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(dictService,"useStatus","btnType","openType","menus");
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.addObject("menuName", menuName);
	  modelAndView.addObject("record", record);
	  modelAndView.addObject("dicts", dicts);
	  modelAndView.setViewName("system/resources/button/add");
	  return modelAndView;
	}
	
	/**
	 * 新增方法
	 * @return
	 */
	@RequestMapping(value = "buttonAdd")
	@RequiresPermissions("resource:button:add")
	public ResponseEntity<Result<Object>> buttonAdd(Button record, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  Result<Object> result = buttonService.saveButton(record, user);
	  return doResult(result);
	}
	
	/**
	 * 菜单修改页面
	 * @return
	 */
	@RequestMapping(value = "buttonUpdateView")
	@RequiresPermissions("resource:button:update")
	public ModelAndView buttonUpdateView(String menuId,String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  //新增字典
	  List<Menu> list = menuService.queryMenusByMenuType(MenuType.GN.getType(),user);
	  DictLoadUtil.addMenuDict(list,"menus");
	  //获取字典信息
	  Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(dictService,"useStatus","btnType","openType","menus");
	  //查询菜单
	  Button button = buttonService.queryButton(id,user); 
	  //获取父节点
	  Menu parentRecord = menuService.queryMenu(button.getMenuId(),user);
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.addObject("menuName", parentRecord.getMenuName());
	  modelAndView.addObject("record", button);
	  modelAndView.addObject("dicts", dicts);
	  modelAndView.setViewName("system/resources/button/update");
	  return modelAndView;
	}
	
	/**
	 * 更新方法
	 * @return
	 */
	@RequestMapping(value = "buttonUpdate")
	@RequiresPermissions("resource:button:update")
	public ResponseEntity<Result<Object>> buttonUpdate(Button record, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  Result<Object> result = buttonService.updateButton(record, user);
	  return doResult(result);
	}
	
}
